
<script setup>
import { onMounted, reactive, ref } from 'vue';
import { ElMessage } from "element-plus";
import { getUserApi, delUserApi } from '@/api/user';
import user from './user.vue'
//时间戳转时间
import dayjs from 'dayjs'
import { useRoleStatus, menuInfoApiList } from '@/hooks'
const menuInfoApi = menuInfoApiList()
const roleStatus = useRoleStatus()
const userRef = ref('')

const queryData = reactive({
  current: 1,
  pageSize: 10,
  create_time: '',
  username: '',
  nickname: '',
  phone: '',
  status: '',
  avatar: '',
  Email: '',
  id: '',
  roleList: ''
})
const total = ref(0)
const list = ref([])
const initData = async () => {
  let { data } = await getUserApi(queryData)
  //  console.log(res);
  data.rows = data.rows.map(item => {
    item.create_time = dayjs(item.create_time).format('YYYY-MM-DD HH:mm:ss')
    return item
  })
  list.value = data.rows
  total.value = data.count
}
onMounted(() => {
  initData()
})
//搜索
const onSearch = () => {
  queryData.current = 1
  initData()
}

//重置
const onReset = () => {
  Object.assign(queryData, {
    current: 1,
    pageSize: 10,
    create_time: '',
    username: '',
    nickname: '',
    phone: '',
    status: '',
    avatar: '',
    Email: '',
    id: '',
    roleList: ''
  })
  initData()
}

//每页数量切换
const sizeChange = (val) => {
  queryData.pageSize = val
  initData()
}
//页码切换
const CurrentChange = val => {
  queryData.current = val
  initData()
}
//编辑修改显示
const editBtn = (id) => {
  userRef.value.isShow = true
  userRef.value.replyData(id)

}
//删除功能
const delRow = async (id) => {
  const res = await delUserApi(id)
  console.log();
  if (!res.success) return ElMessage.error(res.msg)
  ElMessage.success('删除成功')
  if (queryData.current > 1 && list.value.length === 1) queryData.current--
  initData()
}
//处理时间
const timeArr = ref([])
watch(timeArr, (val) => {
  console.log(timeArr.value);
  if (timeArr.value) {
    queryData.create_time = val
      .map((item) => {
        item = +new Date(item)
        return item
      })
      .join(",")
    console.log(queryData.create_time);
  } else {
    queryData.create_time = ''
  }
})

// 处理角色名
const tagsInfo = (list) => {
  return list.map(item => {
    return item.name
  })
}

</script>
<template>
  <div>
    <div class="app-container">
      <span style="font-weight: 700;">用户管理</span> <span>一个管理员可以有多个角色组，左侧的菜单根据管理员所拥有的权限进行生成</span>
      <el-card>
        <!-- 搜索区域 -->
        <el-form label-width="80px" :inline="true" label-position="top">
          <el-form-item label="用户名">
            <el-input slot="prepend" placeholder="请输入" v-model="queryData.username">
            </el-input>
          </el-form-item>

          <el-form-item label="昵称">
            <el-input v-model="queryData.nickname" slot="prepend" placeholder="请输入">
            </el-input>
          </el-form-item>

          <el-form-item label="手机号">
            <el-input v-model="queryData.phone" slot="prepend" placeholder="请输入">
            </el-input>
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="queryData.status" clearable slot="prepend" placeholder="请选择">
              <el-option v-for="item in roleStatus" :key="item.value" :label="item.label" />
            </el-select>

          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker v-model="timeArr" type="daterange" range-separator="至" start-placeholder="开始日期"
              end-placeholder="结束日期" clearable>
            </el-date-picker>
          </el-form-item>

          <el-form-item style="display: flex; align-items: end;">
            <div></div>
            <div>
              <el-button type="primary" @click="onSearch">搜索</el-button>
              <el-button @click="onReset">重置</el-button>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="app-container">
      <el-card>
        <!-- 工具区域 -->
        <el-row type="flex" justify="space-between">
          <div></div>
          <div>
            <el-button type="primary" @click="userRef.isShow = true">新增管理员</el-button>
          </div>
        </el-row>
        <br>
        <!-- 表格区域 -->
        <el-table :data="list" border stripe>
          <el-table-column label="ID" prop="id" width="70px"></el-table-column>
          <el-table-column label="用户名" prop="username"></el-table-column>
          <el-table-column label="昵称" prop="nickname"></el-table-column>
          <el-table-column label="头像" prop="avatar">

            <template #default="{ row }">
              <div>
                <el-image style="width: 50px; height: 50px;" :src="'http://124.223.14.236:8881' + row.avatar"
                  :preview-teleported="true" :preview-src-list="['http://124.223.14.236:8881' + row.avatar]" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="phone"></el-table-column>
          <el-table-column label="Email" prop="email"></el-table-column>
          <el-table-column label="角色名">
            <template #default="{ row }">
              <el-tag type="success" v-for="(item, index) in tagsInfo(row.roleList)" :key="index">{{ item
              }}</el-tag>

            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template #default="{ row }">
              <span v-if="row.status == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="create_time" width="155px"></el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-link class="link" type="primary" :underline="false" @click="editBtn(row.id)">编辑</el-link>
              <el-popconfirm title="确定删除吗？"  @confirm="delRow(row.id)">
                <template #reference>
                  <el-link class="link" :underline="false" type="primary">删除</el-link>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <br>
        <!-- 分页区域 -->
        <el-row type="flex" justify="space-between">
          <div></div>
          <el-pagination v-model:current-page="queryData.current" v-model:page-size="queryData.pageSize"
            :page-sizes="[5, 10, 20]" :small="small" :disabled="disabled" :background="background"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="sizeChange"
            @current-change="CurrentChange" />
        </el-row>
        <!-- 新增修改组件 -->
        <!-- <parking ref="parkingRef" @updateList="initData" /> -->
        <user ref="userRef" @updateList="initData" />
      </el-card>
    </div>
  </div>
</template>

<style scoped>
.link {
  margin-left: 8px;
}
</style>